<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>Reference</title>
    <link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>luamqtt</h1>

<ul>
  <li><a href="../index.html">Index</a></li>
</ul>



<h2>Examples</h2>
<ul class="nowrap">
  <li><a href="../examples/simple.lua.html">simple.lua</a></li>
  <li><a href="../examples/sync.lua.html">sync.lua</a></li>
  <li><strong>mqtt5-simple.lua</strong></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
  <li><a href="../modules/mqtt.html">mqtt</a></li>
  <li><a href="../modules/mqtt.client.html">mqtt.client</a></li>
  <li><a href="../modules/mqtt.ioloop.html">mqtt.ioloop</a></li>
</ul>
<h2>Topics</h2>
<ul class="">
  <li><a href="https://github.com/xHasKx/luamqtt#readme">README</a></li>
  <li><a href="../topics/LICENSE.html">LICENSE</a></li>
</ul>

</div>

<div id="content">

    <h2>mqtt5-simple.lua</h2>
<pre>
<span class="keyword">local</span> mqtt = <span class="global">require</span>(<span class="string">"mqtt"</span>)

<span class="comment">-- create mqtt client
</span><span class="keyword">local</span> client = mqtt.client{
	uri = <span class="string">"mqtt.flespi.io"</span>,
	<span class="comment">-- NOTE: more about flespi tokens: https://flespi.com/kb/tokens-access-keys-to-flespi-platform
</span>	username = <span class="string">"stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9"</span>,
	clean = <span class="keyword">true</span>,
	version = mqtt.v50,
}
<span class="global">print</span>(<span class="string">"created MQTT v5.0 client:"</span>, client)

client:on{
	connect = <span class="keyword">function</span>(connack)
		<span class="keyword">if</span> connack.rc ~= <span class="number">0</span> <span class="keyword">then</span>
			<span class="global">print</span>(<span class="string">"connection to broker failed:"</span>, connack:reason_string(), connack)
			<span class="keyword">return</span>
		<span class="keyword">end</span>
		<span class="global">print</span>(<span class="string">"connected:"</span>, connack) <span class="comment">-- successful connection
</span>
		<span class="comment">-- subscribe to test topic and publish message after it
</span>		<span class="global">assert</span>(client:subscribe{ topic=<span class="string">"luamqtt/#"</span>, qos=<span class="number">1</span>, callback=<span class="keyword">function</span>(suback)
			<span class="global">print</span>(<span class="string">"subscribed:"</span>, suback)

			<span class="comment">-- publish test message
</span>			<span class="global">print</span>(<span class="string">'publishing test message "hello" to "luamqtt/simpletest" topic...'</span>)
			<span class="global">assert</span>(client:publish{
				topic = <span class="string">"luamqtt/simpletest"</span>,
				payload = <span class="string">"hello"</span>,
				qos = <span class="number">1</span>,
				properties = {
					payload_format_indicator = <span class="number">1</span>,
					content_type = <span class="string">"text/plain"</span>,
				},
				user_properties = {
					hello = <span class="string">"world"</span>,
				},
			})
		<span class="keyword">end</span>})
	<span class="keyword">end</span>,

	message = <span class="keyword">function</span>(msg)
		<span class="global">assert</span>(client:acknowledge(msg))

		<span class="global">print</span>(<span class="string">"received:"</span>, msg)
		<span class="global">print</span>(<span class="string">"disconnecting..."</span>)
		<span class="global">assert</span>(client:disconnect())
	<span class="keyword">end</span>,

	<span class="global">error</span> = <span class="keyword">function</span>(err)
		<span class="global">print</span>(<span class="string">"MQTT client error:"</span>, err)
	<span class="keyword">end</span>,
}

<span class="global">print</span>(<span class="string">"running ioloop for it"</span>)
mqtt.run_ioloop(client)

<span class="global">print</span>(<span class="string">"done, ioloop is stopped"</span>)</pre>


</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2020-07-09 23:06:55 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
